package com.kuzlak.cms.handler;

import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

import javax.sql.DataSource;
import java.util.HashMap;

//@EnableTransactionManagement

@Configuration
@MapperScan(basePackages = "com.kuzlak.cms.module.*.mapper")
public class MybatisPlusConfig implements InitializingBean {
    @Autowired
    private DataSource dataSource;
    @Value("${backup.path}")
    private String backupPath;

    @Value("${spring.datasource.username}")
    private String username;

    @Value("${spring.datasource.password}")
    private String password;


/*    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();

        interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());

        return interceptor;
    }*/

    @Bean
    public MyMetaObjectHandler myLogicSqlInjector() {
        return new MyMetaObjectHandler();
    }


    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();

        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
        // 取消MyBatis Plus的最大分页500条的限制
        paginationInnerInterceptor.setMaxLimit(100000L);
        MyDynamicTableNameInterceptor myDynamicTableNameInterceptor = new MyDynamicTableNameInterceptor();
        HashMap<String, MyTableNameHandler> map = new HashMap<String, MyTableNameHandler>(2) {{
            System.out.println("----------------------------------------------------");


            //整个函数返回的结果就是替换后的新表名，这个生成的表名的规则可以自己随便指定

            put("sys_organ", (sql, tableName, student) -> getTableName(sql, tableName, student));

            put("sys_users", (sql, tableName, student) -> getTableName(sql, tableName, student));
            put("sys_role", (sql, tableName, student) -> getTableName(sql, tableName, student));

            put("fcxs_customer", (sql, tableName, student) -> getTableName(sql, tableName, student));
            put("fcxs_house", (sql, tableName, student) -> getTableName(sql, tableName, student));
            put("fcxs_clue", (sql, tableName, student) -> getTableName(sql, tableName, student));
            put("fcxs_visit", (sql, tableName, student) -> getTableName(sql, tableName, student));
            put("fcxs_ financial", (sql, tableName, student) -> getTableName(sql, tableName, student));
            put("fcxs_ paymentplan", (sql, tableName, student) -> getTableName(sql, tableName, student));
            put("fcxs_ schedule", (sql, tableName, student) -> getTableName(sql, tableName, student));
            put("fcxs_order", (sql, tableName, student) -> getTableName(sql, tableName, student));
            put("gzxx_clue", (sql, tableName, student) -> getTableName(sql, tableName, student));
            put("gzxx_work", (sql, tableName, student) -> getTableName(sql, tableName, student));
            put("zscp_course", (sql, tableName, student) -> getTableName(sql, tableName, student));



        }};
        myDynamicTableNameInterceptor.setTableNameHandlerMap(map);
        interceptor.addInnerInterceptor(myDynamicTableNameInterceptor);
        interceptor.addInnerInterceptor(paginationInnerInterceptor);

        return interceptor;


    }

    private String getTableName(String sql, String tableName, Object student) {
        System.out.println("Index  sssssssssssssssssssssssssssssssss");
        System.out.println("-----------------SQL1------------------------------" + sql);
        System.out.println("getTableName:" + tableName);
        String s = " t" + tableName + " ";
        if (sql.indexOf("SELECT") > -1) {

            String regex = "([" + tableName + "][as][" + s + "]+$)";
            System.out.println("getTableName:" + regex);
            System.out.println("getTableName:" + s);
            System.out.println("getTableName:" + sql);


            if (sql.indexOf(s) > -1 && sql.indexOf(s + "_") == -1) {
                System.out.println(" match found.:" + s);
                System.out.println(sql);
                return tableName;
            } else {
                System.out.println("No match found.");
                return tableName + " as x";
            }


        } else {
            return tableName;
        }

    }

    @Override
    public void afterPropertiesSet() throws Exception {
  
    }

    @Bean
    public GlobalConfig globalConfig() {
        GlobalConfig config = new GlobalConfig();
        // 开启自动填充功能
        config.setMetaObjectHandler(new MyMetaObjectHandler());
        return config;
    }
}
